<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class ArticleController extends CommonController {
    public function config(){
        $this->assign('mark','content');
        $this->assign('umark','article_config');
        $this->display('config');
    }
    //列表
    public function index(){
        $info_object = D('info');
        $map['status'] = array('egt', '0'); // 禁用和正常状态
        $Page = new Page($info_object->where($map)->count(),C('ADMIN_CONFIG.page_size'));
        $Page->setConfig('prev','«');
        $Page->setConfig('next','»');
        $data_list     = $info_object
            ->where($map)
            ->order('id asc')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        $this->assign('mark','content');
        $this->assign('umark','article_list');
        A('list')->setMetaTitle('资讯列表')
            ->setBreadCrumb(array('内容','资讯管理','资讯列表'))
            ->addTopButton('add',array('title'=>'资讯添加'))
            ->addTopButton('resume')
            ->addTopButton('delete')
            ->addTableColumn('id','ID','number',50)
            ->addTableColumn('column_id','所属栏目','getcolumnup',100,'false')
            ->addTableColumn('title','资讯标题','text','auto','false')
            ->addTableColumn('abstract','资讯摘要','text','auto','false')
            ->addTableColumn('sort','排序','sort',80)
            ->addTableColumn('status','状态','status',60)
            ->addTableColumn('ctime','创建时间','date',120)
            ->addTableColumn('','操作','rightbtn',200,'false')
            ->setTableDataList($data_list)
            ->setTableDataPage($Page->show())
            ->addRightButton('edit')
            ->addRightButton('forbid')
            ->addRightButton('delete')
            ->fetch();
    }
    //添加
    public function add(){
        $info_object = D('info');
        if (IS_POST) {
            $data = $info_object->create();
            if ($data) {
                $id = $info_object->add($data);
                if ($id) {
                    $ajax = array(
                        'status'=>1,
                        'text'=>'资讯新增成功',
                        'url'=>U('index')
                    );
                } else {
                    $ajax = array(
                        'status'=>0,
                        'text'=>'资讯新增失败',
                        'url'=>''
                    );
                }
            }else {
                $ajax = array(
                    'status'=>0,
                    'text'=>$info_object->getError(),
                    'url'=>''
                );
            }
            $this->ajaxReturn($ajax);
        }else {
            $this->assign('mark','content');
            $this->assign('umark','article_list');
            $updata = array();
            $coldata = M('info_column')->field('id,title')->where(array('status'=>1,'upid'=>0))->select();
            foreach ($coldata as $key => $value) {
                $up['value'] = $value['id'];
                $up['title'] = $value['title'];
                $up['disabled'] = true;
                array_push($updata,$up);
                $subdata = M('info_column')->field('id,title')->where(array('status'=>1,'upid'=>$value['id']))->select();
                foreach ($subdata as $k => $val) {
                    $u['value'] = $val['id'];
                    $u['title'] = '· '.$val['title'];
                    $u['disabled'] = false;
                    array_push($updata,$u);
                }
            }
            A('form')->setMetaTitle('资讯添加')
                ->setBreadCrumb(array('内容','资讯管理','资讯列表','资讯添加'))
                ->setPostUrl(U('add'))
                ->addFormItem('title','text','资讯标题','',array(),array('required'=>true))
                ->addFormItem('column_id','select','所属栏目','',$updata)
                ->addFormItem('abstract','text','资讯摘要','资讯摘要，不超过120字')
                ->addFormItem('content','simditor','主要内容','')
                ->addFormItem('sort','text','排序','排序')
                ->addFormItem('status','switch','状态','',array('启用','禁用'))
                ->setFormData(array('status'=>1,'sort'=>0))
                ->fetch();
        }
    }
    //编辑
    public function edit(){
        $info_object = D('info');
        if (IS_POST) {
            $data = $info_object->create();
            if ($data) {
                $rs = $info_object->save($data);
                if ($rs) {
                    $ajax = array(
                        'status'=>1,
                        'text'=>'资讯编辑成功',
                        'url'=>U('index')
                    );
                } else {
                    $ajax = array(
                        'status'=>0,
                        'text'=>'资讯编辑失败',
                        'url'=>''
                    );
                }
            }else {
                $ajax = array(
                    'status'=>0,
                    'text'=>$info_object->getError(),
                    'url'=>''
                );
            }
            $this->ajaxReturn($ajax);
        }else {
            $this->assign('mark','content');
            $this->assign('umark','article_list');
            $id = I('get.ids',0,'intval');
            $form_data = $info_object->find($id);
            $updata = array();
            $coldata = M('info_column')->field('id,title')->where(array('status'=>1,'upid'=>0))->select();
            foreach ($coldata as $key => $value) {
                $up['value'] = $value['id'];
                $up['title'] = $value['title'];
                $up['disabled'] = true;
                array_push($updata,$up);
                $subdata = M('info_column')->field('id,title')->where(array('status'=>1,'upid'=>$value['id']))->select();
                foreach ($subdata as $k => $val) {
                    $u['value'] = $val['id'];
                    $u['title'] = '· '.$val['title'];
                    $u['disabled'] = false;
                    array_push($updata,$u);
                }
            }
            A('form')->setMetaTitle('资讯编辑')
                ->setBreadCrumb(array('内容','资讯管理','资讯列表','资讯编辑'))
                ->setPostUrl(U('edit'))
                ->addFormItem('id','hidden')
                ->addFormItem('title','text','资讯标题','',array(),array('required'=>true))
                ->addFormItem('column_id','select','所属栏目','',$updata)
                ->addFormItem('abstract','text','资讯摘要','资讯摘要，不超过120字')
                ->addFormItem('content','simditor','主要内容','')
                ->addFormItem('sort','text','排序','排序')
                ->addFormItem('status','switch','状态','',array('启用','禁用'))
                ->setFormData($form_data)
                ->fetch();
        }
    }
    //状态修改
    public function setStatus(){
        if (IS_GET) {
            $ids = I('get.ids');
            $status = I('get.status');
            $info_object = M('info');
            $map = array(
                'id' => array('in',$ids),
            );
            $save = array(
                'status' => $status,
            );
            $rs = $info_object->where($map)->save($save);
            if ($rs) {
                $ajax = array(
                    'status'=>1,
                    'text'=>'资讯状态修改成功',
                    'url'=>U('index')
                );
            } else {
                $ajax = array(
                    'status'=>0,
                    'text'=>'资讯状态修改失败',
                    'url'=>''
                );
            }
            $this->ajaxReturn($ajax);
        }
    }
    //删除
    public function delete(){
        if (IS_GET) {
            $ids = I('get.ids');
            $info_object = M('info');
            $map = array(
                'id' => array('in',$ids),
            );
            $rs = $info_object->where($map)->delete();
            if ($rs) {
                $ajax = array(
                    'status'=>1,
                    'text'=>'资讯删除成功',
                    'url'=>U('index')
                );
            } else {
                $ajax = array(
                    'status'=>0,
                    'text'=>'资讯删除失败',
                    'url'=>''
                );
            }
            $this->ajaxReturn($ajax);
        }
    }

    public function submit(){
        $info_object = D('info');
        $map['status'] = array('eq', '0'); // 禁用状态
        $Page = new Page($info_object->where($map)->count(),C('ADMIN_CONFIG.page_size'));
        $Page->setConfig('prev','«');
        $Page->setConfig('next','»');
        $data_list     = $info_object
            ->where($map)
            ->order('id asc')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        $this->assign('mark','content');
        $this->assign('umark','article_submit');
        A('list')->setMetaTitle('资讯列表')
            ->setBreadCrumb(array('内容','资讯管理','资讯列表'))
            ->addTopButton('resume',array('title'=>'通过审核'))
            ->addTopButton('delete')
            ->addTableColumn('id','ID','number',50)
            ->addTableColumn('column_id','所属栏目','getcolumnup',100,'false')
            ->addTableColumn('title','资讯标题','text','auto','false')
            ->addTableColumn('abstract','资讯摘要','text','auto','false')
            ->addTableColumn('sort','排序','sort',80)
            ->addTableColumn('status','状态','status',60)
            ->addTableColumn('ctime','创建时间','date',120)
            ->addTableColumn('','操作','rightbtn',140,'false')
            ->setTableDataList($data_list)
            ->setTableDataPage($Page->show())
            ->addRightButton('resume',array('title'=>'<i class="icon icon-check"></i> 通过'))
            ->addRightButton('delete')
            ->fetch();
    }
}
